﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Graphics.PackedVector;



namespace WindowsGame
{
	public class EnemiesRenderer
	{
		EnemiesResources enemiesResources;

		IGameLogicService gameLogicService;

		GraphicsDevice graphicsDevice;



		public EnemiesRenderer(GraphicsDevice graphicsDeviceIn, EnemiesResources enemiesResourcesIn,
			IGameLogicService gameLogicServiceIn)
		{
			graphicsDevice = graphicsDeviceIn;
			enemiesResources = enemiesResourcesIn;
			gameLogicService = gameLogicServiceIn;
		}



		public void LoadContent()
		{
		}



		public void Draw(GameTime gameTime)
		{
			Debug.Assert(graphicsDevice != null);
			Debug.Assert(enemiesResources != null);
			Debug.Assert(gameLogicService != null);

			Matrix[] transforms = new Matrix[enemiesResources.model.Bones.Count];
			enemiesResources.model.CopyAbsoluteBoneTransformsTo(transforms);

			foreach(var enemy in gameLogicService.GameState_.enemies.Value)
			{
				foreach(ModelMesh mesh in enemiesResources.model.Meshes)
				{
					foreach(BasicEffect effect in mesh.Effects)
					{
						effect.View = gameLogicService.GameState_.camera.viewMatrix.Value.GetNative();
						effect.Projection = gameLogicService.ProjectionMat;
						effect.World = transforms[mesh.ParentBone.Index] *
										enemy.data.rotationMat.Value.GetNative() * enemy.data.translationMat.Value.GetNative();
					}
					mesh.Draw();
				}
			}
		}
	}
}